package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/user/shoppingCart")
@RestController
public class ShoppingController {
    @Autowired
    private ShoppingService shoppingService;

    /**
     * 添加到购物车
     */
    @PostMapping("/add")
    public Result addInShoppingCar(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加到购物车的信息为：{}",shoppingCartDTO);
        shoppingService.addInShoppingCar(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 查看购物车
     */
    @GetMapping("/list")
    public Result checkShoppingCart(){
        log.info("查询购物车信息");
        List<ShoppingCart> list=shoppingService.checkShoppingCart();
        return Result.success(list);

    }

    /**
     * 删除购物车中一个商品
     */
    @PostMapping("/sub")
    public Result deleteOne(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除一份商品");
        shoppingService.deleteOne(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("clean")
    public Result clean(){
        log.info("清除购物车");
        shoppingService.clean();
        return Result.success();
    }
}
